package xyz.xiaoshengfu.xrpc.data;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.lang3.StringUtils;
import org.springframework.context.support.AbstractApplicationContext;

import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import xyz.xiaoshengfu.xrpc.common.JedisClientPool;
import xyz.xiaoshengfu.xrpc.common.LocalNetworkInformationUtil;
import xyz.xiaoshengfu.xrpc.config.ReferenceBean;
import xyz.xiaoshengfu.xrpc.config.RegistryConfig;
import xyz.xiaoshengfu.xrpc.config.ServiceBean;
import xyz.xiaoshengfu.xrpc.config.XRpcConfig;
import xyz.xiaoshengfu.xrpc.main.XRpcMainApplication;

/**
 * <p>Title: XRpcResources</p>
 * <p>Description: xRPC资源整合类</p>
 * @author xiaoshengfu(2439323118@qq.com)
 * @date 2020年3月24日
 */
public class XRpcResources {

	private static final Map<String, ReferenceBean> referenceBeanMap = new ConcurrentHashMap<String, ReferenceBean>();
	private static final Map<String, Object> localServiceMap = new ConcurrentHashMap<String, Object>();

	public static final XRpcConfig xRpcConfig = new XRpcConfig();
	public static JedisClientPool jedisClientPool;
	public static AbstractApplicationContext abstractApplicationContext;

	public static JedisClientPool getJedisClientPool() {
		return jedisClientPool;
	}

	public static void init() {
		if (abstractApplicationContext != null && xRpcConfig.getApplicationConfig() != null
				&& xRpcConfig.getRegistryConfig() != null) {
			xRpcConfig.getApplicationConfig().setHostAddress(LocalNetworkInformationUtil.getLocalIpv4Address());
			RegistryConfig registryConfig = xRpcConfig.getRegistryConfig();
			if ("redis".equals(registryConfig.getProtocol())) {
				JedisPool jedisPool = null;
				if (StringUtils.isNoneBlank(registryConfig.getPassword())) {
					jedisPool = new JedisPool(new JedisPoolConfig(), registryConfig.getAddress(),
							registryConfig.getPort(), 2000, registryConfig.getPassword());
				} else {
					jedisPool = new JedisPool(registryConfig.getAddress(), registryConfig.getPort());
				}
				jedisClientPool = new JedisClientPool(jedisPool);
			}
			List<ReferenceBean> referenceBeanList = xRpcConfig.getReferenceBeanList();
			if (referenceBeanList.size() > 0) {
				for (ReferenceBean referenceBean : referenceBeanList) {
					referenceBeanMap.put(referenceBean.getReferenceInterface(), referenceBean);
				}
			}
			List<ServiceBean> serviceBeanList = xRpcConfig.getServiceBeanList();
			if (serviceBeanList.size() > 0) {
				for (ServiceBean serviceBean : serviceBeanList) {
					localServiceMap.put(serviceBean.getServiceInterface(),
							abstractApplicationContext.getBean(serviceBean.getRef()));
				}
			}
			new XRpcMainApplication().start();
		} else {
			throw new RuntimeException("Failed to load xRPC configuration information.");
		}
	}

	public static int getReferenceTimeout(String interfaceName) {
		ReferenceBean referenceBean = referenceBeanMap.get(interfaceName);
		if (referenceBean != null) {
			return referenceBean.getTimeout();
		}
		return 600000;
	}

	public static String getLocalApplicationName() {
		return xRpcConfig.getApplicationConfig().getName();
	}

	public static Object getLocalService(String interfaceName) {
		return localServiceMap.get(interfaceName);
	}

}
